const app = getApp()
// pages/account/account.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: {
      id: "ccc",
      username: "方糕",
      sex: 2,
      birthday: "2002-03-03",
      headimg: "/assets/avatars/avatar1.jpg",
      department: "计算机学院",
      address: "铁二"
    },

    sexOptions: ["未知", "男", "女"],
    sexPickerIndex: 1,
    pickerDate: "2021-2-2",
  },

  formSubmit: function (event) {
    let val = event.detail.value;
    // console.log(
    //   "用户名: " + val.username + 
    //   "\n性别: " + val.sex + 
    //   "\n生日: " + val.birthday + 
    //   "\n学院: " + val.dept + 
    //   "\n地址: " + val.addr
    // );
    let username = val.username
    let sex = val.sex
    let birthday = val.birthday
    let dept = val.dept
    let addr = val.addr
    wx.request({
      url: 'http://localhost:8888/account/editAccount',
      method: 'POST',
      data: {
        id: app.globalData.loginAccount,
        username: username,
        sex: sex,
        birthday: birthday,
        department: dept,
        address: addr
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.switchTab({
          url: '../user/user',
        })
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: 'http://localhost:8888/account/getAccount',
      method: 'GET',
      data: {
        id: app.globalData.loginAccount
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          user: res.data.data,
          pickerDate: res.data.data.birthday,
          sexPickerIndex: res.data.data.sex
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarTitle({
      title: "编辑资料"
    });

    var that = this;
    that.setData({
      pickerDate: that.data.user.birthday
    })

  },

  bindDateChange: function (e) {
    this.setData({
      pickerDate: e.detail.value
    });
    console.log(e.detail.value);

  },

  bindSexPickerChange: function (e) {
    this.setData({
      sexPickerIndex: e.detail.value
    })
  },

  changeAvatar() {
    var that = this
    wx.chooseImage({
      count: 1,
      success(res) {
        wx.showLoading({
          title: '上传中',
        })
        var chooseImg = res.tempFilePaths[0]
        wx.uploadFile({
          filePath: chooseImg,
          name: 'file',
          url: 'http://localhost:8888/image/upload',
          success: (res) => {
            let url = JSON.parse(res.data).data;
            wx.request({
              url: 'http://localhost:8888/account/changeAvatar',
              method: 'POST',
              data: {
                id: app.globalData.loginAccount,
                url: url
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                that.setData({
                  user: res.data.data
                })
                wx.hideLoading({
                  success: (res) => {},
                })
                wx.showToast({
                  title: '修改头像成功',
                })
              }
            })
          }
        })
      }
    })
  }
})